package com.ruoyi.loss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.loss.domain.bo.WellCustomFieldsBo;
import com.ruoyi.loss.domain.vo.WellCustomFieldsVo;
import com.ruoyi.loss.domain.WellCustomFields;
import com.ruoyi.loss.mapper.WellCustomFieldsMapper;
import com.ruoyi.loss.service.IWellCustomFieldsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 自定义字段Service业务层处理
 *
 * @author yangyong
 * @date 2023-12-25
 */
@RequiredArgsConstructor
@Service
public class WellCustomFieldsServiceImpl implements IWellCustomFieldsService {

    private final WellCustomFieldsMapper baseMapper;

    /**
     * 查询自定义字段
     */
    @Override
    public WellCustomFieldsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询自定义字段列表
     */
    @Override
    public TableDataInfo<WellCustomFieldsVo> queryPageList(WellCustomFieldsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WellCustomFields> lqw = buildQueryWrapper(bo);
        Page<WellCustomFieldsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询自定义字段列表
     */
    @Override
    public List<WellCustomFieldsVo> queryList(WellCustomFieldsBo bo) {
        LambdaQueryWrapper<WellCustomFields> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WellCustomFields> buildQueryWrapper(WellCustomFieldsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WellCustomFields> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getIsGlobal()), WellCustomFields::getIsGlobal, bo.getIsGlobal());
        lqw.eq(StringUtils.isNotBlank(bo.getWellNo()), WellCustomFields::getWellNo, bo.getWellNo());
        lqw.eq(bo.getWellAttribute() != null, WellCustomFields::getWellAttribute, bo.getWellAttribute());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldCn()), WellCustomFields::getFieldCn, bo.getFieldCn());
        lqw.eq(StringUtils.isNotBlank(bo.getFieldEn()), WellCustomFields::getFieldEn, bo.getFieldEn());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WellCustomFields::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增自定义字段
     */
    @Override
    public Boolean insertByBo(WellCustomFieldsBo bo) {
        WellCustomFields add = BeanUtil.toBean(bo, WellCustomFields.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改自定义字段
     */
    @Override
    public Boolean updateByBo(WellCustomFieldsBo bo) {
        WellCustomFields update = BeanUtil.toBean(bo, WellCustomFields.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WellCustomFields entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除自定义字段
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
